<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Table Sort Class
 *
 * This class is used to sort the result set in ASC or DESC Order based on the  field names
 *
 * @author    Exa
 * @link      --
 */
class Table_Sort {
    
	var $CI;	
	var $unique_id = 'table_gen_sort';	
	
  	/**
	 * Constructor
	 *
	 * @access    public
	 * @param    array    initialization parameters
	 */
	function Table_Sort($params = array())
	{	
		$this->CI =& get_instance();	
		
		if (count($params) > 0)
		{
			$this->initialize($params);        
		}
			
		log_message('debug', "Table Sort Class Initialized");
	}
	
	// --------------------------------------------------------------------
    
    /**
     * Initialize Preferences
     *
     * @access    public
     * @param     array    initialization parameters
     * @return    void
     */
    function initialize($params = array())
    {
        if (count($params) > 0)
        {
            foreach ($params as $key => $val)
            {
                if (isset($this->$key))
                {
                    $this->$key = $val;
                }
            }        
        }
    }
	
	// ------------------------------------------------------------------------	
		
	/**
	 * Sort On
	 *
	 * @access 	public
	 * @return 	void
	 *
	 */
	function sort_on()
	{
		
		$frm = $_POST;
		if(is_array($frm) && count($frm) > 0)
		{
			$base_sort_on 	= $this->CI->input->post('base_sort_on');
			$base_order_by 	= $this->CI->input->post('base_order_by');
			
			$base_sort_on = str_replace('sort_on_','',$base_sort_on);
			
			if(!empty($base_sort_on))
			{
				$sort_on_arr = array('base_sort_on' => $base_sort_on, 'base_order_by' => $base_order_by);
				$this->CI->session->set_userdata($this->unique_id,$sort_on_arr);
			}			
		}
	}
	
	// ------------------------------------------------------------------------	
		
	/**
	 * Get Base Sort On
	 *
	 * If the order by is already set then send the original order by string as default param.
	 * Then check if the table sorter order by is set if yes then return with new order by string.
	 *
	 * @access 	private
	 * @param	string
	 * @return 	string
	 *
	 */
	function _get_base_sort_on($org_sort = '')
	{
		$sort_on_arr 	= $this->CI->session->userdata($this->unique_id);
		if(is_array($sort_on_arr) && count($sort_on_arr) > 0)
		{
			$base_sort_on 	= $sort_on_arr["base_sort_on"];
			$base_order_by 	= $sort_on_arr["base_order_by"];
		
			if(!empty($base_sort_on))
			{
				$org_sort = $base_sort_on .' '.$base_order_by;
			}
		}
		
		return $org_sort;	
	}
	
	// --------------------------------------------------------------------------
	
	/**
	 * Clear Base Sort
	 *
	 * @access private
	 * @return void
	 */
	function _clear_base_sort()
	{
		$sort_on_arr 	= $this->CI->session->userdata($this->unique_id);		
		if(is_array($sort_on_arr) && count($sort_on_arr) > 0)
		{
			$this->CI->session->unset_userdata($this->unique_id);
		}
	}
	
	// ------------------------------------------------------------------------	
		
	/**
	 * Get Base Sort
	 *
	 * @access 	private
	 * @return 	string
	 *
	 */
	function _get_base_sort()
	{
		$base_sort_on = '';
		$sort_on_arr 	= $this->CI->session->userdata($this->unique_id);		
		if(is_array($sort_on_arr) && count($sort_on_arr) > 0)
		{
			$base_sort_on 	= 'sort_on_'.$sort_on_arr["base_sort_on"];			
		}
		return $base_sort_on;
	}
	
	// ------------------------------------------------------------------------	
		
	/**
	 * Get Order By
	 *
	 * @access 	private
	 * @return 	string
	 *
	 */
	function _get_order_by()
	{
		$base_order_by = '';
		$sort_on_arr 	= $this->CI->session->userdata($this->unique_id);		
		if(is_array($sort_on_arr) && count($sort_on_arr) > 0)
		{
			$base_order_by 	= $sort_on_arr["base_order_by"];				
		}
		return $base_order_by;
	}
	
}/* End of Class Table_Sort */
/* Location: ./system/application/libraries/Table_Sort.php */
?>